/*
	Compresor Gzip
    Copyright (C) 2012 Darío López Padial 
    @bukosabino

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
*/

/**
 @file LZcoder.cpp
 @brief Implementación de la Clase LZcoder
 @author Dario Lopez Padial
*/

#include <iostream>
#include "LZcoder.h"

using namespace std;

unsigned int LZ :: Avanza () {
	unsigned int resultado;
	if( indice < v_dinamico.Size() ) {
		if(indice<1){
			resultado = v_dinamico.Get (indice);
			indice++;
		}
		else{
			unsigned int longitud = Busca_Coincidencias();
			if(longitud<3){
				resultado = v_dinamico.Get (indice);
				indice++;
			}
			else{
				resultado = longitud + 256;
				indice = indice + longitud;
			}					
		}
	}
	else{
		resultado = 256;
	}
	return resultado;
}



unsigned int LZ :: Busca_Coincidencias () {
	unsigned int resultado=0;
	unsigned int auxiliar;
	unsigned int aux_indice = indice;
	unsigned int contador=0;
	int k= indice -1;
	distancia=0;
	for(int i=k; i>=0; i--) {
		auxiliar = i;
			while ( (v_dinamico.Get(auxiliar) == v_dinamico.Get(aux_indice)) && (contador < 258) && aux_indice < v_dinamico.Size() ){
				auxiliar++;
				aux_indice++;
				contador++;
			}
			if ( (contador > resultado) && ((indice-i) <= 32768) ){
				resultado = contador;
				distancia = (indice - i);
			}
			contador=0;
			aux_indice=indice;
	}
	return resultado;
}
